package com.mmy.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.sys.TSysAddress;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.sys.TSysAddressServiceImpl;
import com.mmy.service.impl.sys.TSysUserServiceImpl;

@Controller
public class TSysAddressController {
	@Autowired
	TSysUserServiceImpl tSysUserServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	TSysAddressServiceImpl tSysAddressServiceImpl;

	/* 后台游客新建地址 */
	@RequestMapping(value = "/tocreatememberaddress")
	public String toCreateMemberAddress(Model mAddress, HttpServletRequest request) {
		String iUserID = request.getParameter("iUserID");
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mAddress.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mAddress.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mAddress.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);
		mAddress.addAttribute("iUserID", iUserID);
		return "webbackground/membermanagement/createaddress";
	}

	/* 提交创建的收货信息 */
	@RequestMapping(value = "/savacreatememberaddress")
	public String savaCreateMemberAddress(TSysAddress tSysAddtress, HttpSession session) throws Exception {
		/* 获得最大收货编号 */
		Object iAddressID = tSysAddressServiceImpl.getMaxAddressID();
		/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
		if (iAddressID == null) {
			tSysAddtress.setAddressID(1);
		} else {
			tSysAddtress.setAddressID(Integer.parseInt(iAddressID.toString()) + 1);
		}
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		tSysAddtress.setCreateDate(new Date());

		tSysAddtress.setCreator(tSysUser.getUserName());
		tSysAddressServiceImpl.save(tSysAddtress.getAddressID());
		tSysAddressServiceImpl.save(tSysAddtress);

		return "redirect:memberinfo?UserID=" + tSysAddtress.getUserID();

	}

	/**
	 * 函数功能说明：后台 修改游客收货信息
	 *
	 * @param 参数说明 tSysAddress
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	/* 点击修改按钮进入修改页面 */
	@RequestMapping("/toupdatememberaddress")
	public String updateMemberAddressCheckedFind(HttpServletRequest request, Model mAddress) {
		String iAddressID = request.getParameter("iAddressID");
		List<TSysAddress> db_TSysAddress = tSysAddressServiceImpl.findByiAddressID(Integer.parseInt(iAddressID));
		mAddress.addAttribute("newTSysAddress", db_TSysAddress.get(0));
		/* 查询所有区域 国家或者省市区 */
		/* 查询所有区域 洲与国家 */

		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mAddress.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mAddress.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mAddress.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);

		return "webbackground/membermanagement/updateaddress";
	}

	/* 提交修改的收货信息 */
	@RequestMapping(value = "/saveupdatememberaddress")
	public String updateMemberAddress(TSysAddress tSysAddress, HttpSession session) throws Exception {
		int iAddressID = tSysAddress.getAddressID();
		/* 定义一个新的实体类对象 */
		TSysAddress dtSysAddress = tSysAddressServiceImpl.findByiAddressID(iAddressID).get(0);
		dtSysAddress.setName(tSysAddress.getName());
		dtSysAddress.setMobile(tSysAddress.getMobile());
		dtSysAddress.setZoneID(tSysAddress.getZoneID());
		dtSysAddress.setAddress(tSysAddress.getAddress());
		dtSysAddress.setType(tSysAddress.getType());
		dtSysAddress.setPostcode(tSysAddress.getPostcode());
		dtSysAddress.setMemo(tSysAddress.getMemo());
		dtSysAddress.setModifyDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		dtSysAddress.setReviser(tSysUser.getUserName());

		tSysAddressServiceImpl.save(dtSysAddress);

		return "redirect:memberinfo?UserID=" + dtSysAddress.getUserID();

	}

	/**
	 * 函数功能说明： 后台删除收货信息信息
	 *
	 * @param 参数说明 tSysAddress
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	@PostMapping("/deletememberaddresschecked")
	@ResponseBody
	public String deleteMemberAddressChecked(String checkList) {

		String[] striAddressIDs = checkList.split(",");
		List<Integer> iAddressIDs = new ArrayList<>();

		for (String striAddressID : striAddressIDs) {
			iAddressIDs.add(Integer.parseInt(striAddressID));
		}
		int iAddressID = iAddressIDs.get(0);
		TSysAddress dtSysAddress = tSysAddressServiceImpl.findByiAddressID(iAddressID).get(0);
		tSysAddressServiceImpl.deleteChecked(iAddressIDs);

		return "redirect:memberinfo?UserID=" + dtSysAddress.getUserID();

	}

	/* 后台员工新建地址 */
	@RequestMapping(value = "/tocreatestaffaddress")
	public String toCreateStaffAddress(Model mAddress, HttpServletRequest request) {
		String iUserID = request.getParameter("iUserID");
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mAddress.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mAddress.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mAddress.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);
		mAddress.addAttribute("iUserID", iUserID);
		return "webbackground/staffmanagement/createaddress";
	}

	/* 提交创建的收货信息 */
	@RequestMapping(value = "/savacreatestaffaddress")
	public String savaCreateStaffAddress(TSysAddress tSysAddtress, HttpSession session) throws Exception {
		/* 获得最大收货编号 */
		Object iAddressID = tSysAddressServiceImpl.getMaxAddressID();
		/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
		if (iAddressID == null) {
			tSysAddtress.setAddressID(1);
		} else {
			tSysAddtress.setAddressID(Integer.parseInt(iAddressID.toString()) + 1);
		}
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		tSysAddtress.setCreateDate(new Date());

		tSysAddtress.setCreator(tSysUser.getUserName());
		tSysAddressServiceImpl.save(tSysAddtress.getAddressID());
		tSysAddressServiceImpl.save(tSysAddtress);

		return "redirect:staffinfo?UserID=" + tSysAddtress.getUserID();

	}

	/**
	 * 函数功能说明：后台 修改员工收货信息
	 *
	 * @param 参数说明 tSysAddress
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	/* 点击修改按钮进入修改页面 */
	@RequestMapping("/toupdatestaffaddress")
	public String updateStaffAddressCheckedFind(HttpServletRequest request, Model mAddress) {
		String iAddressID = request.getParameter("iAddressID");
		List<TSysAddress> db_TSysAddress = tSysAddressServiceImpl.findByiAddressID(Integer.parseInt(iAddressID));
		mAddress.addAttribute("newTSysAddress", db_TSysAddress.get(0));
		/* 查询所有区域 国家或者省市区 */
		/* 查询所有区域 洲与国家 */

		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mAddress.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mAddress.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mAddress.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);

		return "webbackground/staffmanagement/updateaddress";
	}

	/* 提交修改的收货信息 */
	@RequestMapping(value = "/saveupdatestaffaddress")
	public String updateStaffAddress(TSysAddress tSysAddress, HttpSession session) throws Exception {
		int iAddressID = tSysAddress.getAddressID();
		/* 定义一个新的实体类对象 */
		TSysAddress dtSysAddress = tSysAddressServiceImpl.findByiAddressID(iAddressID).get(0);
		dtSysAddress.setName(tSysAddress.getName());
		dtSysAddress.setMobile(tSysAddress.getMobile());
		dtSysAddress.setZoneID(tSysAddress.getZoneID());
		dtSysAddress.setAddress(tSysAddress.getAddress());
		dtSysAddress.setType(tSysAddress.getType());
		dtSysAddress.setPostcode(tSysAddress.getPostcode());
		dtSysAddress.setMemo(tSysAddress.getMemo());
		dtSysAddress.setModifyDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		dtSysAddress.setReviser(tSysUser.getUserName());

		tSysAddressServiceImpl.save(dtSysAddress);

		return "redirect:staffinfo?UserID=" + dtSysAddress.getUserID();

	}

	/**
	 * 函数功能说明： 删除收货员工信息信息
	 *
	 * @param 参数说明 tSysAddress
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	@PostMapping("/deletestaffaddresschecked")
	@ResponseBody
	public String deleteStaffAddressChecked(String checkList) {

		String[] striAddressIDs = checkList.split(",");
		List<Integer> iAddressIDs = new ArrayList<>();

		for (String striAddressID : striAddressIDs) {
			iAddressIDs.add(Integer.parseInt(striAddressID));
		}
		int iAddressID = iAddressIDs.get(0);
		TSysAddress dtSysAddress = tSysAddressServiceImpl.findByiAddressID(iAddressID).get(0);
		tSysAddressServiceImpl.deleteChecked(iAddressIDs);

		return "redirect:staffinfo?UserID=" + dtSysAddress.getUserID();

	}
}
